package com.erp.framework.util.logger;

import java.util.ArrayList;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.util.user.User;

public class LoggerDelegate extends SimpleSearchMaintStyleDelegate {

    LoggerRemote contr = null;

    protected Object getEjbManager() {

        if (this.contr == null) {
            try {
                LoggerHome home = null;
                home = (LoggerHome) ServiceLocator.getInstance().getHome(ProjectConstants.LOGGER_EJB, LoggerHome.class);
                this.contr = home.create();
            } catch (java.lang.reflect.UndeclaredThrowableException ue) {
                Logger.logExceptions(this, ue);
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
        return this.contr;
    }

    protected Class getDAOClass() {

        return LoggerDao.class;
    }

    public void updateTable(String functionName, String operation, ArrayList models, User user) throws Exception {

        System.out.println("LoggerDelegate: super_class=" + this.getClass().getSuperclass().getName());
        try {
            // SimpleRemote remote = (SimpleRemote) getMasterManager();
            System.out.println("LoggerDelegate is null? " + (getEjbManager() == null));

            System.out.println("LoggerDelegate.updateTable(), functionName=" + functionName);
            System.out.println("LoggerDelegate.updateTable(), operation=" + operation);
            System.out.println("LoggerDelegate.updateTable(), size of Models=" + models.size());
            System.out.println("LoggerDelegate.updateTable(), models is null?" + (models == null));
            System.out.println("LoggerDelegate.updateTable(), user is null?" + (user == null));

            ((LoggerRemote) getEjbManager()).updateTable(functionName, operation, models, user);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw e;
        }
    }

    public void setEJBLogLevel() throws Exception {

        try {
            LoggerRemote remote = (LoggerRemote) getEjbManager();
            remote.setEJBLogLevel();
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw e;
        }
    }

    public void setEJBLogTimestamp() throws Exception {

        try {
            LoggerRemote remote = (LoggerRemote) getEjbManager();
            remote.setEJBLogTimestamp();
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw e;
        }
    }

    public void setEJBLogToFile() throws Exception {

        try {
            LoggerRemote remote = (LoggerRemote) getEjbManager();
            remote.setEJBLogToFile();
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            throw e;
        }
    }

}